﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2018/7/20
 * 时间: 19:12
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace MiniTextViewer
{
	/// <summary>
	/// Description of FileManager.
	/// </summary>
	public class FileManager
	{
		static Dictionary<string, FileManager> instance = new Dictionary<string, FileManager>();
		
		public static FileManager GetInstance(string instName)
		{
			if (!instance.ContainsKey(instName))
			{
				instance.Add(instName, new FileManager(instName));
			}
			return instance[instName];
		}
		
		string configFile;
		Dictionary<string, FilePosition> positions;
		
		FileManager(string instName)
		{
			string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MiniTextViewer";
			if (!Directory.Exists(path))
				Directory.CreateDirectory(path);
			
			configFile = path + "\\" + instName + ".ini";
		}
		
		public void Load()
		{
			positions = new Dictionary<string, FilePosition>();
			
			if (!File.Exists(configFile))
				return;
			
			var sr = new StreamReader(configFile, Encoding.UTF8);
			string content = sr.ReadToEnd();
			sr.Close();
			
			var ssr = new StringReader(content);
			string line = null;
			while ((line = ssr.ReadLine()) != null)
			{
				string[] fields = line.Split(new char[] {'\t'}, StringSplitOptions.None);
				if (fields.Length == 3)
				{
					string fname = fields[0];
					int lineNum = 0;
					int linePosition = 0;
					int.TryParse(fields[1], out lineNum);
					int.TryParse(fields[2], out linePosition);
					
					var pos = new FilePosition(fname, lineNum, linePosition);
					if (!positions.ContainsKey(fname))
					{
						positions.Add(fname, pos);
					}
					else
					{
						positions[fname] = pos;
					}
				}
			}
			ssr.Close();
		}
		
		public void Save()
		{
			var sw = new StreamWriter(configFile, false, new UTF8Encoding(false));
			foreach (var pair in positions)
			{
				string line = string.Format("{0}\t{1}\t{2}", pair.Value.Filename, pair.Value.LineNum, pair.Value.LinePosition);
				sw.WriteLine(line);
			}
			sw.Close();
		}
		
		public string[] GetFileLines(string filename)
		{
			Encoding enc = TxtFileEncoder.GetEncoding(filename);
			var sr = new StreamReader(filename, enc);
			string content = sr.ReadToEnd();
			sr.Close();
			
			var lines = new List<string>();
			var ssr = new StringReader(content);
			string line = null;
			while ((line = ssr.ReadLine()) != null)
			{
				lines.Add(line);
			}
			ssr.Close();
			
			return lines.ToArray();
		}
		
		public FilePosition GetFilePosition(string filename)
		{
			if (!positions.ContainsKey(filename))
				positions.Add(filename, new FilePosition(filename, 0, 0));
			return positions[filename];
		}
		
		public void UpdateFilePosition(FilePosition pos)
		{
			UpdateFilePosition(pos.Filename, pos.LineNum, pos.LinePosition);
		}
		
		public void UpdateFilePosition(string filename, int lineNum, int linePosition)
		{
			FilePosition pos = GetFilePosition(filename);
			pos.LineNum = lineNum;
			pos.LinePosition = linePosition;
		}
	}
}
